<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="base-site-site" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";

const { service } = useCool();

// cl-upsert
const Upsert = useUpsert({
	items: [
		{ prop: "name", label: "标题", required: true, component: { name: "el-input" } },
		{ prop: "introduce", label: "描述", component: { name: "el-input" } },
		{
			prop: "mainImage",
			label: "图片",
			component: { name: "cl-upload", props: { isSpace: true, limit: 1 } },
			required: true
		},
		{
			prop: "tagId",
			label: "标签id",
			hook: {
				bind: ["split", "number"], // 通道流程，分割 -> 转成number -> 绑定值
				submit: ["join"]
			},
			component: {
				name: "el-select",
				options: [],
				props: {
					multiple: true,
					"multiple-limit": 3
				}
			}
		},
		{ prop: "address", label: "链接", required: true, component: { name: "el-input" } }
	],
	async onOpen() {
		service.base.site.type.list().then((res) => {
			Upsert.value?.setOptions(
				"tagId",
				res.map((e) => {
					return {
						label: e.name || "",
						value: e.id
					};
				})
			);
		});
	}
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ prop: "id", label: "ID" },
		{ prop: "name", label: "标题" },
		{
			prop: "introduce",
			label: "描述"
		},
		{ prop: "mainImage", label: "图片", component: { name: "cl-image", props: { size: 60 } } },
		{ prop: "createTime", label: "创建时间", sortable: "desc", width: 160 },
		{ prop: "updateTime", label: "更新时间", sortable: "custom", width: 160 },
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.base.site.site
	},
	(app) => {
		app.refresh();
	}
);
</script>
